package com.crm.controller;

import com.crm.dto.DictDto;
import com.crm.entity.Dict;
import com.crm.result.ActivityResult;
import com.crm.result.PageResult;
import com.crm.result.Result;
import com.crm.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("system/dict/type")
@RestController
@Slf4j
public class DictController {
    @Autowired
    private DictService dictService;
    //分页查询字典表
    @GetMapping("/list")
    public ActivityResult pageDict(DictDto dictDto){
        PageResult res = dictService.pageDict(dictDto);
        return ActivityResult.success("查询成功", null, res.getRows(), res.getTotal());
    }

    //新增字典
    @PostMapping
    public Result addDict(@RequestBody DictDto dictDto){
        dictService.add(dictDto);
        return Result.success();
    }

    //删除字典
    @DeleteMapping("/{idsStr}")
    public Result deleteDict(@PathVariable String idsStr){
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::valueOf)
                .collect(Collectors.toList());
        dictService.deleteBatchByIds(ids);
        return Result.success();
    }

    //根据dictId查询字典信息
    @GetMapping("/{dictId}")
    public Result<Dict> seletDictById(@PathVariable Long dictId){
        Dict dict = dictService.selectDictById(dictId);
        return Result.success(dict);
    }

    //修改字典信息
    @PutMapping
    public Result updateDict(@RequestBody Dict dict){
        dictService.updateDict(dict);
        return Result.success();
    }



}
